package com.demo.websocket.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

	// 这里配置了以“/app”开头的websocket请求url。和名为“endpointSang”的endpoint(端点)
	@Override
	public void configureMessageBroker(MessageBrokerRegistry config) {
		// System.err.println("configureMessageBroker");
		config.enableSimpleBroker("/topic");
		// config.setApplicationDestinationPrefixes("/app");
	}

	@Override
	public void registerStompEndpoints(StompEndpointRegistry registry) {
		// System.err.println("registerStompEndpoints");
		registry.addEndpoint("/endpoint").withSockJS();
	}

	@Bean
	public SocketSessionRegistry SocketSessionRegistry() {
		return new SocketSessionRegistry();
	}

	@Bean
	public STOMPConnectEventListener STOMPConnectEventListener() {
		return new STOMPConnectEventListener();
	}

}
